package com.yunfei.interceptorApi;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.interceptorApi.contants.InterceptorApiContants;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
 * @Description: 权限控制
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/interceptor")
public class InterceptorApiController {
    @Autowired
    private Environment env;
    @PostMapping(value="/get")
    @ResponseBody
    public Object get(){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        String value=env.getProperty(InterceptorApiContants.MAXEXPIRED_KEY);
        if(!StringUtil.isNullOrEmpty(value)&&compareVersion(value,"0")>0){
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }else{
            resultModel.setCode(ResultStatus.NOROLE.getCode());
            resultModel.setMessage(ResultStatus.NOROLE.getMessage());
        }
        return resultModel;

    }
    /**
     * 版本号比较
     *
     * @param v1
     * @param v2
     * @return 0代表相等，1代表左边大，-1代表右边大
     * Utils.compareVersion("1.0.358_20180820090554","1.0.358_20180820090553")=1
     */
    public static int compareVersion(String v1, String v2) {
        if (v1.equals(v2)) {
            return 0;
        }
        String[] version1Array = v1.split("[._]");
        String[] version2Array = v2.split("[._]");
        int index = 0;
        int minLen = Math.min(version1Array.length, version2Array.length);
        long diff = 0;

        while (index < minLen
                && (diff = Long.parseLong(version1Array[index])
                - Long.parseLong(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            for (int i = index; i < version1Array.length; i++) {
                if (Long.parseLong(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Long.parseLong(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }

}
